﻿using Basic.FlowCollect;
using Basic.FlowExtendService;
using Basic.FlowRemoteModel.Flow.Model;
using Basic.FlowService.Interface;
using WeDonekRpc.Helper;
namespace Basic.FlowService.Extend
{
    internal class FlowFormService : IFlowFormService
    {
        private readonly IFlowNodeCollect _FlowNode;
        private readonly IFlowCollect _Flow;
        private readonly IFormService _Form;

        public FlowFormService ( IFlowNodeCollect flowNode,
            IFlowCollect flow,
            IFormService form )
        {
            this._FlowNode = flowNode;
            this._Flow = flow;
            this._Form = form;
        }

        public long[] GetFormIds ( long flowId )
        {
            long? defId = this._Flow.GetDefFormId(flowId);
            long[] formId = this._FlowNode.GetsFormId(flowId);
            if ( defId.HasValue )
            {
                return formId.Insert(defId.Value, 0);
            }
            return formId;
        }

        public FlowFormDto[] GetForms ( long flowId )
        {
            long? defId = this._Flow.GetDefFormId(flowId);
            long[] formId = this._FlowNode.GetsFormId(flowId);
            if ( defId.HasValue )
            {
                formId = formId.Insert(defId.Value, 0);
            }
            if ( formId.IsNull() )
            {
                return null;
            }
            Dictionary<long, string> names = this._Form.GetName(formId);
            return formId.ConvertAll(a => new FlowFormDto
            {
                FormId = a,
                FormName = names.GetValueOrDefault(a),
                IsDefault = defId.HasValue && defId.Value == a
            });
        }
    }
}
